package LeetCode.leetcode90;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        boolean[] used = new boolean[nums.length];
        backTracking(path,res,0,used,nums);
        return res;
    }
    public static void backTracking(List<Integer> path,List<List<Integer>> res,int startIndex,boolean[] used,int[] nums){
        res.add(new ArrayList<>(path));
        if (startIndex == nums.length){
            return;
        }
        for (int i = startIndex; i < nums.length; i++) {
            if (i>0 && nums[i] == nums[i-1] && !used[i - 1])
                continue;
            path.add(nums[i]);
            used[i] = true;
            backTracking(path,res,i+1,used,nums);
            used[i] = false;
            path.remove(path.size()-1);
        }
    }
}
